package cn.tedu.coolshark.mapper;

import cn.tedu.coolshark.entity.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProductMapper {
    @Insert("insert into product values(null,#{title},#{url},#{price},#{oldPrice},0,#{saleCount},#{created},#{categoryId})")
    void insertById(Product product);

    @Select("select id,title,url,price,old_price,sale_count,url from product")
    @Result(property = "saleCount",column = "sale_count")
    @Result(property = "oldPrice",column = "old_price")
    List<Product> selectIndex(Product product);

    @Delete("delete from product where id=#{id}")
    void delete(int id);

    @Select("select url from product where id=#{id}")
    String selectUrlById(int id);

    @Select("select id,title,price,sale_count,url from product")
    @Result(property = "saleCount",column = "sale_count")
    List<Product> select();

    @Select("select id,title,sale_count from product order by sale_count desc limit 0,6")
    @Result(property = "saleCount",column = "sale_count")
    List<Product> selectTop();

    @Select("select id,title,url,price,old_price,sale_count,url from product where category_id=#{cid}")
    @Result(property = "oldPrice",column = "old_price")
    @Result(property = "saleCount",column = "sale_count")
    List<Product> SelectCategory(int cid);


    @Select("select id,title,url,price,old_price,sale_count,url from product where title  like concat('%',#{wd},'%')" )
    @Result(property = "oldPrice",column = "old_price")
    @Result(property = "saleCount",column = "sale_count")
    List<Product> selectByWd(String wd);

    @Select("select id,title,url,price,old_price,sale_count,created,view_count from product where id=#{id}")
    @Result(property = "oldPrice",column = "old_price")
    @Result(property = "saleCount",column = "sale_count")
    @Result(property = "viewCount",column = "view_count")
    Product selectById(int id);

    @Update("update product set view_count=view_count+1 where id=#{id}")
    void updateViewCount(int id);
}
